<?php

namespace app\common\model;

use basic\ModelBasic;

/**
 * 提现模型
 */
class Withdraw extends ModelBasic
{
	use \traits\ModelTrait;

	/** 平台每日最大自动到账提现金额 */
	CONST DAY_MAX_AMOUNT = 100000;

	/** 平台每日最大自动到账提现笔数 */
	CONST DAY_MAX_TRANS = 100;

    /** 微信支付单笔限额 */
    CONST ITEM_MAX_AMOUNT_LMT = 5000;

    /** 微信支付单用户每日限额 */
    CONST ITEM_SUM_AMOUNT_LMT = 20000;

	//SchoolEnroll::$pay_type
		/**
	 * 设置where条件
	 * @Author   FW
	 * @DateTime 2021-01-12
	 * @Email    123763712@qq.com
	 * @param    [type]           $where [description]
	 * @param    string           $alias [description]
	 * @param    [type]           $model [description]
	 */
	public static function setWhere($where,$alias='m',$model=null){
        $model = $model===null ? new self() : $model;

        $model->alias($alias);

        if ( isset($where['org_id']) && $where['org_id'] > 0 ) {
            $org_id = $where['org_id'] + 0;
        } else {
            $org_id = static::getBelongOrganId();
        }
        if ( $org_id > 0 ) {
            $model->where(sprintf('%s.org_id=%d' , $alias , $org_id));
        }

        // if ( isset($where['school_id']) && $where['school_id'] > 0 ) {
        //     $school_id = (int)$where['school_id'];
        // } else {
        //     $school_id = static::getBelongSchoolId();
        // }

        // if ( $school_id > 0 ) {
        //     $model->where($alias.'.school_id',$school_id);
        // }

        if ( static::$softDelete ) {
        	$model->where($alias.'.is_del',0);
        }
        return $model;
    }

    /**
     * 列表
     * @Author   FW
     * @DateTime 2020-09-24
     * @Email    123763712@qq.com
     * @param    [type]
     * @return   [type]
     */
    public static function getList($where){
        $count = self::setWhere($where)->count();
        $data  = [];
        if ( $count == 0 ) {
            return compact('data','count');
        }

        $data=self::setWhere($where,'m')
        ->field('m.*,org.organ_name,wa.wd_reciver_realname')
        ->join('__ORGAN__ org','m.org_id=org.id' , 'left')
        ->join('__WITHDRAW_ACCOUNT__ wa','m.wd_type=wa.wd_type AND m.receipt_account = wa.wd_reciver_account' , 'left')
        ->page($where['page'] ?? 1,$where['limit'] ?? 10)
        ->select();

        if ( !empty($data) ) {
            $data = $data->toArray();
            $data = static::process_list($data);
        }

        return compact('data','count');
    }

    /**
     * 生成提现流水号
     * @Author   FW
     * @DateTime 2021-07-05
     * @Email    123763712@qq.com
     * @return   [type]           [description]
     */
    public static function createWithdrawTransacSeq ($wd_type)
    {
        return $wd_type . date('YmdHis') . mt_rand(1000,9999);
    }

    /**
     * 机构当天累计提现总额
     * @Author   FW
     * @DateTime 2021-07-06
     * @Email    123763712@qq.com
     * @param    [type]           $org_id [description]
     * @return   [type]                   [description]
     */
    public static function dayTotalAmount ($org_id , $wd_type = 'wechat')
    {
        return self::where('create_time' , 'BETWEEN' , [date('Y-m-d 00:00:00') , date('Y-m-d 23:59:59')])->where(['org_id' => $org_id , 'wd_type' => $wd_type])->sum('amount');
    }
}//Class End
